package com.hxc.sercurity.configu.projo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 韩先楚
 */
@NoArgsConstructor
@Data
public class LoginUserDetails implements UserDetails {

    private User user;

    //封装的当前用户的权限信息
    private List<String> auth;

    public LoginUserDetails(User user, List<String> auth) {
        this.user = user;
        this.auth = auth;
    }

    // 获取用户的权限
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        List<SimpleGrantedAuthority> list =new ArrayList<>();
//        // 遍历用户的权限
//        for (String s : auth) {
//            // 将权限添加到列表中
//            list.add(new SimpleGrantedAuthority(s));
//        }
//        return list;
      if (auth.isEmpty() || auth ==null)
          return null;

        return auth.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    @Override
    // 获取用户的密码
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    // 获取用户的用户名
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    // 判断用户的账户是否未过期
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    // 判断用户的账户是否未锁定
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    // 判断用户的凭证是否未过期
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    // 判断用户是否启用
    public boolean isEnabled() {
        Integer enable = user.getEnable();
        if ( enable == null || enable == 0)
              return false;
           else
               return true;
    }
}
